﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Exentials.Snw.SnwConnector;
using Exentials.Snw.Functions;

namespace SnwConnectorSample
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the DLL library version
            // If the sapnwrfc and related ddl are for a wrong platform or not reachable SnwConnectorException is throwed
            var info = SnwConnection.GetLibraryInfo();
            Console.WriteLine("The sapnwrfc.ddl version is : {0} ", info["Version"]);

            SnwConnection con = null;

            // Create a connection: Uncommend the following row and Provide yours connection parameters
            //con = new SnwConnection(<Host>, <SysNumber>, <Client>, <rfcusername>, <password>, <language>);

            // The ping method is used to test if connection is open 
            Console.WriteLine("Ping result before Open method: {0} ", con.Ping());

            con.Open();


            Console.WriteLine("Ping result after Open method: {0} ", con.Ping());
            Console.WriteLine();


            // Manually create a function calling            
            SnwFunction bapi = new SnwFunction("BAPI_CCODE_GET_FIRSTDAY_PERIOD", con);
            Console.WriteLine("Manual function: {0} ", bapi.Name);

            bapi.SetParameter("COMPANYCODEID", "ACIT");
            bapi.SetParameter("FISCAL_PERIOD", (SnwNumeric)"02");
            bapi.SetParameter("FISCAL_YEAR", (SnwNumeric)"2010");
            bapi.Execute();

            Console.WriteLine("Date: {0:d}", bapi.GetParameter<SnwDate>("FIRST_DAY_OF_PERIOD").ToDateTime());
            SnwStructure ret = bapi.GetParameter<SnwStructure>("RETURN");
            Console.WriteLine("Number: {0}", ret.GetParameter<SnwNumeric>("NUMBER", 3));
            Console.WriteLine("Message: {0}", ret.GetParameter<string>("MESSAGE"));

            // Use a Strongly Typed class function calling
            BapiCcodeGetFirstdayPeriod getFirstDayPeriod = new BapiCcodeGetFirstdayPeriod(con);
            Console.WriteLine("Strongly typed function: {0}", getFirstDayPeriod.Name);

            getFirstDayPeriod.Import.Companycodeid = "ACIT";
            getFirstDayPeriod.Import.FiscalPeriod = "02";
            getFirstDayPeriod.Import.FiscalYear = "2010";
            getFirstDayPeriod.Execute();

            Console.WriteLine("Date: {0:d}", getFirstDayPeriod.Export.FirstDayOfPeriod.ToDateTime());
            Console.WriteLine("Number: {0}", getFirstDayPeriod.Export.Return.Number);
            Console.WriteLine("Message: {0}", getFirstDayPeriod.Export.Return.Message);

            Console.WriteLine();

            // Get results from Tables
            Console.WriteLine("Getting a structure definition using the typed version of RFC_GET_STRUCTURE_DEFINITION");
            Console.WriteLine();

            RfcGetStructureDefinition getStructureDefinition = new RfcGetStructureDefinition(con);
            string structureName = "MARA";
            getStructureDefinition.Import.Tabname = structureName;

            Console.WriteLine("Structure definition of: {0}", structureName);

            getStructureDefinition.Execute();

            // Same results but with typed properties
            foreach (var f in getStructureDefinition.Tables.Fields)
            {
                // Console.Write(f.TabName.PadRight(12)); // always TabName 
                Console.Write(f.Fieldname.PadRight(20));
                Console.Write(f.Position.ToString().PadLeft(4));
                Console.Write(f.Offset.ToString().PadLeft(8));
                Console.Write(f.Intlength.ToString().PadLeft(4));
                Console.Write(f.Decimals.ToString().PadLeft(4));
                Console.Write(f.Exid.PadLeft(2));
                Console.WriteLine();
            }

            // Creating a Purchase order
            BapiPoCreate1 poCreate = new BapiPoCreate1(con);

            poCreate.Import.Poheader.PurchOrg = "0001";
            poCreate.Import.Poheaderx.PurchOrg = "X";
            poCreate.Import.Poheader.DocType = "NB";
            poCreate.Import.Poheaderx.DocType = "X";
            poCreate.Import.Poheader.PurGroup = "001";
            poCreate.Import.Poheaderx.PurGroup = "X";
            poCreate.Import.Poheader.Vendor = "0001002170";
            poCreate.Import.Poheaderx.Vendor = "X";

            var row = poCreate.Tables.Poitem.AddRow();
            var rowx = poCreate.Tables.Poitemx.AddRow();


            row.PoItem = new SnwNumeric("10");
            rowx.PoItem = new SnwNumeric("10");

            row.Material = "AX00056";
            rowx.Material = "X";

            row.Quantity = 40;
            rowx.Quantity = "X";

            poCreate.Execute();

            foreach (var r2 in poCreate.Tables.Return)
            {
                Console.WriteLine(r2.Message);
            }

            Console.WriteLine("Execute commit.");
            // Execute commit
            BapiTransactionCommit commit = new BapiTransactionCommit(con);
            commit.Import.Wait = "X";
            commit.Execute();

            con.Close();

            Console.ReadLine();
        }
    }
}
